//1.判断token,有token，说明
loadInitData();
function loadInitData() {
    var token = localStorage.getItem("token");
    console.log("token:"+token);
    $.ajax({
        type: "get",
        url: "index",
        headers: {'token': token},
        success: function (result) {
            console.log(result)
            var data = result.data;
            console.log("map:" + data);
            var loginCustomer = data.loginCustomer;
            var hotMovieList = data.hotMovieList;
            var exceptMovieList = data.exceptMovieList;
            console.log("用户id:" + customerId);
            if (loginCustomer != null) {
                //弹出优惠券
                var myModal=document.getElementById("myModal");
                myModal.style.display="block";
                var customerId = loginCustomer.customerId;
                var url = "order?customerId=" + customerId;
                $(".head-user").hide();
                $(".user-menu").html('<li class="text"><a href='+url+'>个人中心</a></li>\n' +
                    '                 <li class="text" id="return"><button style="border: none">退出登录</button></li>');
                $(".user-info").show();
            } else {
                $(".head-user").show();
                $(".user-info").hide();
            }
            $("#return").click(function (){
                var token = localStorage.removeItem("token");
                window.location.href = "http://localhost:8080/demo/index.jsp";
            })
            var url1 = "orderby";
            $("#bangdan").html('<a href=' + url1 + '>榜单</a>')
            $(".hot_movie").html("");
            $(".hot_movie").append("<h2>\n" +
                "            正在热映\n" +
                "            <a href=\"\">全部></a>\n" +
                "        </h2>");
            var movieNum = hotMovieList.length;
            if (hotMovieList.length > 8)
                movieNum = 8;
            for (var i = 0; i < movieNum; i++) {
                var hmovieCname = hotMovieList[i].movieCname;
                var moviePicture = hotMovieList[i].moviePicture;
                var hmovieId = hotMovieList[i].movieId;
                var url = "MovieDetail?movieId=" + hmovieId;
                var proEle = '<div class="movie">' +
                    '            <a href=' + url + ' class="a1"></a>' +
                    '            <img src="' + moviePicture + '" alt="">' +
                    '            <div class="movieId">' +
                    '                <p>' + hmovieCname + '</p>' +
                    '            </div>' +
                    '            <div class="movie-score">' +
                    '                <i>' + 9.1 + '</i>' +
                    '            </div>' +
                    '            <div class="goupiao">' +
                    '                <a href=' + url + '>购票</a>' +
                    '            </div>' +
                    '        </div>';
                $(".hot_movie").append(proEle);
            }

            var fmovieNum = exceptMovieList.length;
            if (exceptMovieList.length > 8)
                fmovieNum = 8;
            $(".future_movie").html("");
            $(".future_movie").append("<h2>\n" +
                "            即将上映\n" +
                "            <a href=\"\">全部></a>\n" +
                "        </h2>");
            console.log("预售电影个数：" + fmovieNum)
            for (var j = 0; j < fmovieNum; j++) {
                var fmovieCname = exceptMovieList[j].movieCname;
                var fmoviePicture = exceptMovieList[j].moviePicture;
                var fmovieId = exceptMovieList[j].movieId;
                var furl = "MovieDetail?movieId=" + fmovieId;
                var fproEle = '<div class="fmovie">' +
                    '            <a href=' + furl + '></a>' +
                    '            <img src="' + fmoviePicture + '" alt="">' +
                    '            <div class="movieId">' +
                    '                <p>' + fmovieCname + '</p>' +
                    '            </div>' +
                    '               <div class="yu">' +
                    '            <div class="wantwatch">' +
                    '            <span>36071</span>' +
                    '            <span>人想看</span>' +
                    '            </div>' +
                    '            <a href="">预告片</a>' +
                    '           <a href="">预售</a>' +
                    '           </div>' +
                    '           <div class="sy_riqi">' +
                    '           <i>9</i>' +
                    '           <span>月</span>' +
                    '           <i>2</i>' +
                    '           <span>号上映</span>' +
                    '               </div></div>';

                $(".future_movie").append(fproEle);
            }
            console.log(exceptMovieList)
            var paihangNum = exceptMovieList.length;
            if (exceptMovieList.length > 10)
                paihangNum = 10;
            $(".right-mov").html("");

            var aa = "<div class=\"today_sort\">\n" +
                "            <h2>昨日票房</h2>\n" +
                "        </div>\n" +
                "        <a  href='MovieDetail?movieId=9' class=\"today_first\">\n" +
                "            <div class=\"to_image\">\n" +
                "                <i></i>\n" +
                "            </div>\n" +
                "            <div class=\"to_mov_name\">\n" +
                "                <div class=\"to_mov_name1\">\n" +
                "                    <span>大白鲨</span>\n" +
                "                    <div class=\"to_mov_num\">\n" +
                "                        <span>203.52</span>\n" +
                "                        <span>万</span>\n" +
                "                    </div>\n" +
                "                </div>\n" +
                "            </div>\n" +
                "        </a>"
            $(".right-mov").append(aa)
            console.log("排行电影个数：" + paihangNum)
            var arr = ["177.22", "122.41", "95.16", "84.12", "81.34", "75.61", "62.15", "60.84", "55.64", "50.12"];
            var num = [2,3,4,5,6,7,8,9,10]
            for (var k = 0; k <paihangNum;k++){
                var paihangCname = exceptMovieList[k].movieCname;
                var piaofang = arr[k];
                var num1 = num[k]
                var url2 = "MovieDetail?movieId=" + exceptMovieList[k].movieId;
                console.log(url2)
                var paihangEle = " <a  class=\"to_second\" href="+ url2 +">\n" +
                    "       <span class=\"tow\">\n" +
                    "        <i>"+ num1 +"</i>\n" +
                    "        <span>&nbsp;&nbsp;</span>\n" +
                    "        <span >"+paihangCname+"</span>\n" +
                    "       </span>\n" +
                    "            <div class=\"to_sec_name\">\n" +
                    "                <span>"+ piaofang +"</span>\n" +
                    "                <span> 万</span>\n" +
                    "            </div>\n" +
                    "        </a>"
                $(".right-mov").append(paihangEle);
            }
            //    优惠券
            var couponList=data.couponlist;
            var myModal=document.getElementById("myModal");

            if(couponList==null||couponList.length==0){
                myModal.style.display="none";
            }
            $(".order-box").html("");
            for (var i=0;i<couponList.length;i++){
                $(".order-box").append('<div class="order-header">\n' +
                    '                            <span class="couId" hidden="hidden">'+couponList[i].couponId+'</span>\n' +
                    '                            <span>&nbsp;</span>'+
                    '                            <img src="img/profile_coupon_logo.png" alt="">\n' +
                    '                        </div>')
            }


        }
    })
}
// $(".search-img").click(function (){
//     var text = localStorage.setItem(".text")
//  //   window.location.replace("pages/query.jsp")
// })

window.onload = function(){
    //获取弹窗按钮
    // var btn=document.getElementById("comment-entry");
    var close=document.getElementsByClassName("close")[0];
    console.log("close:"+close)
    var myModal=document.getElementById("myModal");
    //按钮绑定事件
    // btn.onclick=function () {
    //     //获取弹窗
    //     myModal.style.display="block";
    // }
    close.onclick=function () {
        myModal.style.display="none";
    }
    var btn1=document.getElementsByClassName("fabuBtn")[0];
    btn1.onclick=function (){
        var couId=document.querySelector(".couId").textContent;
        console.log("=============")
        console.log(couId)
        var token=localStorage.getItem("token");
        console.log(token)
        $.ajax({
            type: "get",
            url: "receiveCoupon",
            headers: {'token': token},
            data:{
                couId:couId
            },
            success: function (result) {
                console.log(result)
                myModal.style.display="none";
            }
        })
    }
}


